package com.yi.student.daoImpl;

import java.util.List;
import org.konghao.basic.peresist.BaseDao;
import org.springframework.stereotype.Repository;

import com.yi.student.dao.IStudentDao;
import com.yi.student.model.Student;

@Repository("studentDao")
public class StudentDaoImpl extends BaseDao<Student> implements IStudentDao {

	@Override
	public void addUsers(List<Student> students) {
		int index = 0 ;
		for(Student s : students){
			add(s);
			if(index ++ == 0){
				this.getSession().clear() ;
				index = 0;
			}
		}
	}

	@Override
	public boolean checkStuExits(String xh) {

		String hsql = "select stu from Student where stu.xh = ?" ;
		Long id = (Long)this.loadObjByHQL(hsql, xh);
		if(id != null && id > 0){
			return true;
		}
		return false;
	}

	
	@Override
	public List<Student> listPunishStudents(Integer year, String sesemath) {

		String hql = "select stu from Student stu join RewardAndPunish rp where  rp.stu_id = stu.xh and stu.isreword = -1" ;
		if(year != null && year > 0){
			hql += " and rp.year = "+ year ;
		}
		if(sesemath != null && !"".equals(sesemath)){
			hql += " and rp.semester = " + sesemath ;
		}
		return this.list(hql);
	}

	@Override
	public List<Student> listRewardStudents(Integer year, String sesemath) {

		String hql = "select stu from Student stu join RewardAndPunish rp where  rp.stu_id = stu.xh and stu.isreword = 1" ;
		if(year != null && year > 0){
			hql += " and rp.year = "+ year ;
		}
		if(sesemath != null && !"".equals(sesemath)){
			hql += " and rp.semester = " + sesemath ;
		}
		return this.list(hql);
	}

	@Override
	public Student findStudentByXh(String xh) {
		String hql = "select stu from Student stu where stu.xh=?";
		Student stu = this.loadByHql(hql, xh);
		return stu;
	}

	
}
